package com.ssps.back.platform.service.Impl;

import com.ssps.back.manage.permission.MyUtils;
import com.ssps.back.platform.service.UserServcie;
import com.ssps.entity.*;
import com.ssps.mapper.RoleMapper;
import com.ssps.mapper.UserMapper;
import com.ssps.mapper.UserRoleMapper;
import org.apache.tomcat.util.buf.UEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author li
 * @time 2020/07/21 19:57:30
 */

@Service
public class UserServiceImpl implements UserServcie {

    @Autowired
    UserMapper userMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public List<User> findAll(String status, String acount) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if (status!=""&&status!=null) {
            criteria.andStatusLike("%"+status+"%");
        }
        if (acount!=""&&acount!=null) {
            criteria.andAcountLike("%"+acount+"%");
        }
        List<User> userList = userMapper.selectByExample(example);
        for (User user : userList) {
            UserRoleExample example1=new UserRoleExample();
            UserRoleExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andUidEqualTo(user.getUid());
            List<UserRoleKey> userRoleKeyList=userRoleMapper.selectByExample(example1);
            UserRoleKey userRoleKey=userRoleKeyList.size()==0? null:userRoleKeyList.get(0);
            Role role=new Role();
            if (userRoleKey==null){
                role=null;
            }else {
                role = roleMapper.selectByPrimaryKey(userRoleKey.getRid());
            }
            user.setRole(role);
        }
        return userList;
    }


    @Override
    public void saveUser(User user,Integer rid) {
        userMapper.insert(user);
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAcountEqualTo(user.getAcount());
        List<User> userList=userMapper.selectByExample(example);
        User user1=userList.size()==0? null:userList.get(0);
        UserRoleKey userRoleKey=new UserRoleKey();
        userRoleKey.setRid(rid);
        userRoleKey.setUid(user1.getUid());
        userRoleMapper.insert(userRoleKey);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public User findOne(String acount) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAcountEqualTo(acount);
        List<User> userList=userMapper.selectByExample(example);
        return userList.isEmpty() ? null : userList.get(0);
    }

    @Override
    public User FindOneUser(String phone) {
        UserExample example=new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(phone);
        List<User> userList=userMapper.selectByExample(example);
        return userList.isEmpty() ? null : userList.get(0);
    }

    @Override
    public List<Permission> getTrees(User loginUser) {
       List<Permission>list =  userMapper.getPermission(loginUser.getUid());
        return MyUtils.getTree(list);
    }
}
